
package com.example.xsssglxt.controller;
        import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
        import cn.afterturn.easypoi.excel.entity.ExportParams;
        import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
        import cn.afterturn.easypoi.view.PoiBaseView;
        import cn.hutool.core.collection.CollUtil;
        import cn.hutool.poi.excel.ExcelReader;
        import cn.hutool.poi.excel.ExcelUtil;
        import cn.hutool.poi.excel.ExcelWriter;
        import com.alibaba.excel.EasyExcel;
        import com.example.xsssglxt.entity.DomiteryManage;
        import com.example.xsssglxt.entity.Student;
        import com.example.xsssglxt.mapper.DomiteryManageMapper;
        import com.example.xsssglxt.mapper.StudentMapper;
        import com.example.xsssglxt.mapper.SushemanagerMapper;
        import com.example.xsssglxt.service.DomitaryService;
        import com.example.xsssglxt.service.StudentIService;
        import com.example.xsssglxt.service.StudentService;
        import io.swagger.annotations.ApiOperation;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.ModelMap;
        import org.springframework.web.bind.annotation.*;
        import org.springframework.web.multipart.MultipartFile;

        import javax.annotation.Resource;
        import javax.servlet.ServletOutputStream;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import javax.xml.transform.Result;
        import java.io.IOException;
        import java.io.InputStream;
        import java.net.URLEncoder;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

@RestController
@Controller
@RequestMapping("/domitary")
public class DomitaryController {
    @Autowired
    private SushemanagerMapper sushemanagerMapper;
    @Autowired
    private DomitaryService domitaryService;
    @PostMapping("/add")
    public Map<String,Object> save(@RequestBody DomiteryManage domiteryManage){
        Map<String,Object> res=new HashMap<>();
        if(domitaryService.student(domiteryManage)==1){
            res.put("code",200);
            res.put("msg","操作成功");
        }
        return res;
    }
    @PostMapping("/xgxs")
    public Map<String,Object> updatexs(@RequestBody DomiteryManage domiteryManage){
        Map<String,Object> res=new HashMap<>();
        System.out.println(domitaryService.change(domiteryManage));
        if(domitaryService.change(domiteryManage) == 1){
            res.put("code",200);
            res.put("msg","修改成功");
        }
        return res;
    }
    @DeleteMapping("/{id}")
    public  Map<String,Object> delete(@PathVariable Integer id){
        Integer col=sushemanagerMapper.deletxs(id);
        Map<String,Object> res=new HashMap<>();

        res.put("data", col);
        return res;
    }
    @GetMapping("/index")
    public Map<String,Object> index(@RequestParam Integer pageNum, @RequestParam Integer pageSize , @RequestParam String name, @RequestParam String number) {
        pageNum=(pageNum-1)*pageSize;

        List<DomiteryManage> data= sushemanagerMapper.selectPage(pageNum,pageSize,name,number);
        Integer total = sushemanagerMapper.selectTotal(name,number);
        Map<String, Object> res = new HashMap<>();
        res.put("data",data);
        res.put("total",total);


        return  res;
    }
    @GetMapping("/domiindex")
    public Map<String,Object> index2() {


        List<DomiteryManage> data= sushemanagerMapper.selectlist();

        Map<String, Object> res = new HashMap<>();
        res.put("data",data);


        return  res;
    }
    @GetMapping("/export")
    @ApiOperation(value = "导出会员列表Excel")

    public void exportMemberList(ModelMap map,
                                 HttpServletRequest request,
                                 HttpServletResponse response) {

        List<Student> memberList = LocalJsonUtil.getListFromJson("/student.json", Student.class);
        ExportParams params = new ExportParams("会员列表", "会员列表", ExcelType.XSSF);
        map.put(NormalExcelConstants.DATA_LIST, memberList);
        map.put(NormalExcelConstants.CLASS, Student.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "memberList");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }
    @GetMapping("/down")
    public Result down(HttpServletResponse response) throws IOException {
        List<DomiteryManage> domiteryManageList=sushemanagerMapper.selectlist();
        System.out.println(domiteryManageList);
        response.setContentType("application/octet-stream");
        String fileName= URLEncoder.encode("宿舍管理员","UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(),DomiteryManage.class).autoCloseStream(Boolean.FALSE).sheet("宿舍管理员").doWrite(domiteryManageList);


        return null;
    }
//    @GetMapping("/indown")
//    public Result upload(@RequestParam("files") MultipartFile file) throws IOException{
//        EasyExcel.read(file.getInputStream(),DomiteryManage.class,new DataListener(sushemanagerMapper)).sheet().doRead();
//        return null;
//
//    }



}
